/********************************************************************
	created:	2006/01/05
	created:	5:1:2006   13:49
	file base:	dapdef
	file ext:	cpp
	author:		Albert Gu
	
	purpose:	common routine implement
*********************************************************************/
#ifndef _LINUX_OS_
#include "..\stdafx.h"
#endif
#include "dapdef.h"

using namespace std;
/********************************************************************
    Global function implement
*********************************************************************/
void TrimLeft(string& str, char c)
/********************************************************************
	Remove left hand character c.
*********************************************************************/
{
	if(str.empty()) return;
	if(isspace(c))
	{
		string::iterator pos = str.begin();
		while(pos != str.end() && isspace(*pos))
			++pos;
		str.erase(str.begin(),pos);
	}
	else
	{
		string::size_type iPos = str.find_first_not_of(c);
		str.erase((string::size_type)0,iPos);
	}
}

void TrimRight(string& str, char c)
/********************************************************************
	Remove right hand character c.
*********************************************************************/
{
	if(str.empty()) return;
	if(isspace(c))
	{
		string::reverse_iterator rpos = str.rbegin();
		while(rpos != str.rend() && isspace(*rpos))
			++rpos;
		string::iterator pos = rpos.base();
		str.erase(pos,str.end());
	}
	else
	{
		string::size_type iPos = str.find_last_not_of(c);
		str.erase(++iPos);
	}
}

void Replace(string& strSrc, const char* lpszSrc, const char* lpszTar)
/********************************************************************
    Replace substr lpszSrc in source string with target substr lpszTar.
*********************************************************************/
{
	if(!lpszSrc || !lpszTar) return;
	string::size_type iPos = string::npos;
	while(string::npos != (iPos = strSrc.find(lpszSrc)))
		strSrc.replace(iPos,strlen(lpszSrc), lpszTar);
}

void ReplaceEscape(string& str, bool bGet)
{
	if(str.empty()) return;
	if(bGet)
	{
		Replace(str,"\\t","\t");
		Replace(str,"\\n","\n");
		Replace(str,"\\b","\b");
		Replace(str,"LT","<");
		Replace(str,"GT",">");
		Replace(str,"AMP","&");
	}
	else
	{
		Replace(str,"\t","\\t");
		Replace(str,"\n","\\n");
		Replace(str,"\b","\\b");
		Replace(str,"<","LT");
		Replace(str,">","GT");
		Replace(str,"&","AMP");
	}
}
